package com.gph.saviorframework.auth.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gph.saviorframework.Constants;
import com.gph.saviorframework.FieldType;
import com.gph.saviorframework.auth.dao.AuthSystemDao;
import com.gph.saviorframework.auth.model.AuthSystem;
import com.gph.saviorframework.auth.service.AuthSystemService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by root on 16-7-21.
 */
public class AuthSystemServiceImpl implements AuthSystemService {

    @Autowired
    private AuthSystemDao authSystemDao;

    private Map<String, String> fields = new HashMap<String, String>();

    public AuthSystemServiceImpl(){
        fields.put("SYS_ID", FieldType.STRING);
        fields.put("SYS_NAME",FieldType.STRING);
    }

    /**
     * 根据系统名称判断是否已经存在
     *
     * @param sysName
     * @return
     */
    public boolean checkExistSystem(String sysName) {
        if(authSystemDao.countByName(sysName)>0){
            return true;
        }
        return false;
    }

    /**
     * 根据系统名称获取AuthSystem
     *
     * @param sysName
     * @return
     */
    public AuthSystem getByName(String sysName) {
        return authSystemDao.getByName(sysName);
    }

    /**
     * 保存
     *
     * @param authSystem 对象
     */
    public void save(AuthSystem authSystem) {
        authSystemDao.save(authSystem);
    }

    /**
     * 根据编号获取
     *
     * @param id
     * @return
     */
    public AuthSystem get(String id) {
        return authSystemDao.get(id);
    }

    /**
     * 更新
     *
     * @param authSystem
     * @return
     */
    public void update(AuthSystem authSystem) {
        authSystemDao.update(authSystem);
    }

    /**
     * 批量更新
     *
     * @param authSystems
     */
    public void update(Set<AuthSystem> authSystems) {
        for(AuthSystem authSystem : authSystems){
            update(authSystem);
        }
    }

    /**
     * 根据条件返回数据
     *
     * @param sort
     * @param dir
     * @param params
     * @return
     */
    public List<Map<String, Object>> find(String sort, String dir, Map<String, Object> params) {
        return authSystemDao.find(sort, dir, genWhereClause(params));
    }

    /**
     * 带分页参数的，给bootstrap使用的
     *
     * @param start  开始页码，从1开始
     * @param limit  每页查询的次数
     * @param sort
     * @param dir
     * @param params
     * @param extras
     * @return
     */
    public Map<String, Object> find(int start, int limit, String sort, String dir, Map<String, Object> params, Object... extras) {
        Map<String,Object> map = new HashMap<String, Object>();

        PageHelper.startPage(start, limit);
        List<Map<String,Object>> list = find(sort, dir, params);
        PageInfo pageInfo = new PageInfo(list);

        map.put(Constants.DEFAULT_RECORD_MODEL_KEY, list);
        map.put(Constants.DEFAULT_COUNT_MODEL_KEY, pageInfo.getPages());
        map.put(Constants.DEFAULT_RECORDS_MODEL_KEY,pageInfo.getSize());
        map.put(Constants.DEFAULT_SUCCESS_KEY, Boolean.TRUE);

        return map;
    }

    /**
     * 统计数量
     *
     * @param params
     * @return
     */
    public long count(Map<String, Object> params) {
        return authSystemDao.count(genWhereClause(params));
    }

    /**
     * 根据主键删除
     *
     * @param id
     */
    public void delete(String id) {
        authSystemDao.deleteById(id);
    }

    /**
     * 批量删除
     *
     * @param ids
     */
    public void deleteByIds(String[] ids) {
        for(String id : ids){
            delete(id);
        }
    }

    /**
     * 查询字段集合
     *
     * @return
     */
    public Map<String, String> getFields() {
        return fields;
    }

    /**
     * 根据id查询map
     *
     * @param id
     * @return
     */
    public Map<String, Object> findById(String id) {
        return authSystemDao.findById(id);
    }

    /**
     * 生成where条件
     *
     * @param params
     * @param extra
     * @return
     */
    public String genWhereClause(Map<String, Object> params, Object... extra) {
        String whereClause = "1=1";
        if (params!=null && params.keySet().size() > 0) {
            for (String key : params.keySet()) {
                if (fields.get(key).equals(FieldType.STRING)) {
                    whereClause += " AND S." + key + " LIKE '"+params.get(key)+"'";
                } else {
                    whereClause += " AND S." + key + " ='"+params.get(key)+"'";
                }
            }
        }

        return  whereClause;
    }
}
